package com.yc.test;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import java.io.File;

public class UploadToOSS {
    public static void main(String[] args) {
        // 阿里云 OSS 相关配置信息
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";  // OSS 外网节点或内网节点
        String accessKeyId = "LTAI5tMaMpqNrQHS5ums69Mr";
        String accessKeySecret = "qrCGpsNCirvOSM8wrhnj97hSMO41qw";

        // 本地文件路径（示例，实际情况中应该是从前端接收到的文件路径）
        String filePath = "C:\\Users\\001\\OneDrive\\图片\\1.jpg";

        // OSS 存储桶名称和存储路径
        String bucketName = "daiwei001";
        String objectName = "1.jpg";  // OSS 中保存的路径和文件名

        // 创建 OSS 客户端
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 从本地文件路径读取文件内容
            File file = new File(filePath);

            // 上传文件到阿里云 OSS
            ossClient.putObject(bucketName, objectName, file);

            // 打印上传成功信息
            System.out.println("File uploaded successfully to OSS");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭 OSS 客户端
            ossClient.shutdown();
        }
    }
}
